package primary.primary0;

public class S0069x的平方根 {
    /**
     * 92
     * 二分法
     */
    public int mySqrt(int x) {
        int l = 0;
        int r = x;
        int ans = -1;
        while(l <= r){
            int mid = (l + r) / 2;
            // 不能是(long)(mid * mid), 因为会先计算两个int相乘，会溢出
            if((long)mid * mid <= x){
                ans = mid;
                l = mid + 1;
            }else{
                r = mid - 1;
            }
        }
        return ans;
    }
}
